﻿// Autore: Emanuele Poggi

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using NUnit.Framework;
using RecipesManager.Data;
using RecipesManager.Data.Operations;
using TechTalk.SpecFlow;
using White.Core.Factory;
using White.Core.UIItems;
using White.Core.UIItems.ListBoxItems;
using White.Core.UIItems.WindowItems;

namespace RecipesManager.Specs.Ospite
{
    [Binding, Scope(Feature = "StatisticheOspite")]
    class StatisticheOspite_Steps:OspiteBdd
    {
        Window _dialog;
        Window winstat;
        TextBox EventDate;
        ComboBox Recipes;
        TextBox NTimes;
        Button _ok;

        [BeforeScenario]
        public new void SetUpScenario()
        {
            base.SetUpScenario();
        }

        [AfterScenario]
        public new void TearDownScenario()
        {
            base.TearDownScenario();
        }

        [AfterStep]
        [BeforeStep]
        public void MiniStop()
        {
            MainWindow.WaitWhileBusy();
            MainWindow.ReloadIfCached();
            MainWindow.ReInitialize(InitializeOption.NoCache);
        }

        void GetDialogUi()
        {
            _dialog = MainWindow.ModalWindows()[0];
            _ok = _dialog.Get<Button>("2");
        }

        void GetStatgUi()
        {
            winstat = Application.GetWindow("Visualizza statistiche del ospite corrente");
            EventDate = winstat.Get<TextBox>("PART_TextBox");
            NTimes = winstat.Get<TextBox>("tb_numberTimes");
            Recipes = winstat.Get<ComboBox>("cb_recipes");
        }

        [Then(Regex = "appare un messaggio")]
        public void AppareUnMessaggio()
        {
            Assert.That(MainWindow.ModalWindows().Count() == 1);
            //GetDialogUi();         
        }

        [Then(Regex = "confermo")]
        public void Confermo()
        {
            Assert.That(_ok.Enabled);

            _ok.Click();
        }

        [Then(Regex = "appare una finestra")]
        public void AppareFinestra()
        {
            GetStatgUi();
        }

        [When(Regex = "clicco sul pulsante statistiche")]
        public void AproStatistiche()
        {
           Assert.That( ViewStat.Enabled );
           ViewStat.Click();
        }

        [Given(Regex = "l'ospite \"([a-zA-Z ]+)\" esiste")]
        public void DatoEsisteOspite(string ospite)
        {
            Manager.Guests.Add(ospite);
        }

        [Given(Regex = "la ricetta \"([a-zA-Z ]+)\" esiste")]
        public void DatoEsisteRicetta(string ricetta)
        {
            Manager.Recipes.Add(ricetta);
        }

        [Given(Regex = "esiste evento con i valori")]
        public void DatoEsisteEvento(Table table)
        {
            var nome_evento = "Evento";
            var data = DateTime.Now;
            var invitati = new LinkedList<string>();
            var ricette = new LinkedList<string>();

            foreach (var tableRow in table.Rows)
            {
                if (tableRow["Attributi"].Equals("Nome"))
                {
                     nome_evento = tableRow["Valori"];
                }
                else if (tableRow["Attributi"].Equals("Data"))
                {
                     data = DateTime.ParseExact(tableRow["Valori"], "dd/MM/yyyy",
                                                        CultureInfo.InvariantCulture, DateTimeStyles.None);
                }

                else if (tableRow["Attributi"].Equals("Invitato"))
                {
                    invitati.AddLast(tableRow["Valori"]);
                }

                else if (tableRow["Attributi"].Equals("Ricetta"))
                {
                    ricette.AddLast(tableRow["Valori"]);
                }

                else
                {
                    Assert.Fail("Table");
                }
            }

            var e = Manager.Events.Add(nome_evento, "", data);

            foreach (var i in invitati)
                e.Guests.Add(i);

            foreach (var r in ricette)
                e.Recipes.Add(r);

            Operations.RegisterStats(e);
        }

        [Then(Regex = "vedo i valori")]
        public void VedoStatistiche(Table table)
        {
            var nome_ricetta = "Ricetta";
            var data = DateTime.Now;
            var num = "0";

            foreach (var tableRow in table.Rows)
            {
                if (tableRow["Attributi"].Equals("Nome"))
                {
                    nome_ricetta = tableRow["Valori"];
                    Console.WriteLine( Recipes.SelectedItemText );
                    //Assert.That(Recipes.SelectedItemText.Equals( nome_ricetta));
                }
                else if (tableRow["Attributi"].Equals("Data"))
                {
                    data = DateTime.ParseExact(tableRow["Valori"], "dd/MM/yyyy",
                                                       CultureInfo.InvariantCulture, DateTimeStyles.None);

                    Console.WriteLine(EventDate.Text);
                    var sUIdata = getDataString(EventDate.Text);
                    
                    var UIdata = DateTime.ParseExact(sUIdata, "dd/MM/yyyy",
                                                       CultureInfo.InvariantCulture, DateTimeStyles.None);

                    Console.WriteLine(sUIdata);
                    //Assert.That(data.Equals(UIdata));
                }
                else if (tableRow["Attributi"].Equals("Numero"))
                {
                    num = tableRow["Valori"];
                    Console.WriteLine(NTimes.Text);
                    //Assert.AreEqual(NTimes.Text, num);
                }
                else
                {
                    Assert.Fail("Table");
                }
            }
        }

        [When(Regex = "clicco sull' ospite \"([a-zA-Z ]+)\"")]
        public void VedoOspite(String ospite)
        {
            NavigateToGuests();
            GetGuestsUi();
            Assert.That(Guests.Items.Exists(item => item.Text.Equals(ospite)));
            Guests.Select(ospite);
            GetSingleGuestUi();
        }

        string getDataString( String longDate)
        {
            const string pattern = "[a-zA-Zì]+ ([0-9]+) ([a-zA-Z]+) ([0-9]+)";

            var expression = new Regex(pattern);

            var splitted = expression.Split(longDate);

            Console.WriteLine(splitted.Count());
            Console.WriteLine(splitted[1]);
            Console.WriteLine(splitted[2]);
            Console.WriteLine(splitted[3]);
            Assert.That( splitted.Count() >= 3 );

            var day = splitted[1];
            var month = splitted[2];
            var year = splitted[3];

            switch ( month )
            {
                case "gennaio": month = "01"; break;

                case "febbraio": month = "02"; break;

                case "marzo": month = "03"; break;

                case "aprile": month = "04"; break;

                case "maggio": month = "05"; break;

                case "giugno": month = "06"; break;

                case "luglio": month = "07"; break;

                case "agosto": month = "08"; break;

                case "settembre": month = "09"; break;

                case "ottobre": month = "10"; break;

                case "novembre": month = "11"; break;

                case "dicembre": month = "12"; break;

                default: Assert.Fail("Invalid month");
                    break;
            }

            Console.WriteLine(day + "/" + month + "/" + year);
            if( day.Length==1 )
                return "0"+day + "/" + month + "/" + year;
            return day + "/" + month + "/" + year;
        }
    }
}
